const routerConfig = [
    {
        path: '/',
        redirect: '/home' // 自動跳轉到首頁
    },
    // 登錄頁面
    {
        path: '/login',
        name: 'login', // 路由名稱
        component: () => import('@/pages/login/loginPage.vue') // 懶加載組件
    },
    // 主布局框架
    {
        path: '/home',
        name: 'home',
        redirect: '/workbench',
        component: () => import('@/pages/home/homePage.vue'),
        children: [
            // ========== 首頁相關路由 ==========
            ...createRoutes('home', '首页', [
                // 參數說明: (路徑, 組件名, 頁面標題, [可選]麵包屑)
                {
                    path: 'workbench',
                    component: 'workbenchPage',
                    title: '工作台'
                },
                { path: 'schedule', component: 'schedulePage', title: '日程待辦' }
            ]),

            // ========== 組織架構相關路由 ==========
            ...createRoutes('organization', '組織', [
                { path: 'contacts', component: 'contactsPage', title: '通訊錄', breadcrumb: ['組織架構', '通訊錄'] },
                { path: 'orgManagement', component: 'orgManagement', title: '組織管理', breadcrumb: ['組織架構', '組織管理'] },
                { path: 'orgChart', component: 'orgChart', title: '組織架構圖', breadcrumb: ['組織架構', '組織架構圖'] },
                { path: 'workLocations', component: 'workLocations', title: '工作地點', breadcrumb: ['組織架構', '工作地點'] },
                { path: 'positionManagement', component: 'positionManagement', title: '職位管理', breadcrumb: ['職位體系', '職位管理'] },
                { path: 'jobManagement', component: 'jobManagement', title: '職務管理', breadcrumb: ['職位體系', '職務管理'] },
                { path: 'rankManagement', component: 'rankManagement', title: '職級管理', breadcrumb: ['職位體系', '職級管理'] },
                { path: 'jobCategories', component: 'jobCategories', title: '職位類別', breadcrumb: ['職位體系', '職位類別'] },
                { path: 'roleManagement', component: 'roleManagement', title: '角色管理', breadcrumb: ['角色管理'] },
                { path: 'orgTypes', component: 'orgTypes', title: '組織類型', breadcrumb: ['設置', '組織類型'] },
                { path: 'contactFields', component: 'contactFields', title: '通訊錄字段設置', breadcrumb: ['設置', '通訊錄字段設置'] }
            ]),

            // ========== 員工管理相關路由 ==========
            ...createRoutes('performance', '員工', [
                { path: 'roster', component: 'rosterPage', title: '花名冊', breadcrumb: ['員工管理', '花名冊'] },
                { path: 'employessFiles', component: 'employessFiles', title: '員工檔案', breadcrumb: ['員工管理', '員工檔案'] },
                { path: 'onboardingManagement', component: 'onboardingManagement', title: '入職管理', breadcrumb: ['員工關係', '入職管理'] },
                { path: 'probationConversionManagement', component: 'probationConversionManagement', title: '轉正管理', breadcrumb: ['員工關係', '轉正管理'] },
                { path: 'personnelTransfer', component: 'personnelTransfer', title: '人事調動', breadcrumb: ['員工關係', '人事調動'] },
                { path: 'separationManagement', component: 'separationManagement', title: '離職管理', breadcrumb: ['員工關係', '離職管理'] },
                { path: 'personnelChangeRecords', component: 'personnelChangeRecords', title: '異動記錄', breadcrumb: ['員工關係', '異動記錄'] },
                { path: 'contractManagement', component: 'contractManagement', title: '合同管理', breadcrumb: ['員工關係', '合同管理'] },
                { path: 'employeeCare', component: 'employeeCare', title: '員工關懷', breadcrumb: ['員工關係', '員工關懷'] },
                { path: 'empOverview', component: 'empOverview', title: '員工概況', breadcrumb: ['統計分析', '員工概況'] },
                { path: 'trendAnalysis', component: 'trendAnalysis', title: '趨勢分析', breadcrumb: ['統計分析', '趨勢分析'] },
                { path: 'activeEmp', component: 'activeEmp', title: '在職員工分析', breadcrumb: ['統計分析', '在職員工分析'] },
                { path: 'onboarding', component: 'employeeOnboarding', title: '入職員工分析', breadcrumb: ['統計分析', '入職員工分析'] },
                { path: 'offboarding', component: 'employeeOffboarding', title: '離職員工分析', breadcrumb: ['統計分析', '離職員工分析'] },
                { path: 'transfer', component: 'employeeTransfer', title: '員工調動分析', breadcrumb: ['統計分析', '員工調動分析'] }
            ]),

            ...createRoutes('Attendance', '考勤', [
                { path: 'AttendanceManagement', component: 'AttendanceManagement', title: '考勤組管理', breadcrumb: ['考勤管理', '考勤組管理'] },
                { path: 'ShiftManagement', component: 'ShiftManagement', title: '考勤組管理', breadcrumb: ['考勤管理', '考勤組管理', '新增考勤組'] },
                { path: 'ClassesManage', component: 'ClassesManage', title: '考勤管理', breadcrumb: ['考勤管理', '班次管理',] },
                { path: 'classesAdd', component: 'classesAdd', title: '考勤管理', breadcrumb: ['考勤管理', '班次管理', '新增班次'] },

            ]),

            // ========== 系統設置相關路由 ==========
            ...createRoutes('setting', '系統設置', [
                { path: 'enterpriseInfo', component: 'enterpriseInfo', title: '企業信息' },
                { path: 'adminManagement', component: 'adminManagement', title: '管理員管理', breadcrumb: ['權限管理', '管理員管理'] },
                { path: 'permissionGroup', component: 'permissionGroup', title: '權限組管理', breadcrumb: ['權限管理', '權限組管理'] },
                { path: 'articleManagement', component: 'articleManagement', title: '文章管理' },
                { path: 'operationLog', component: 'operationLog', title: '操作日誌' },
            ]),

            // ========== 資產相關路由 ==========
            ...createRoutes('training', '資產', [
                { path: 'trainProgramMan', component: 'trainProgramMan', title: '培訓計劃管理' },
                { path: 'courseMan', component: 'courseMan', title: '課程管理' },
                { path: 'gridMan', component: 'gridMan', title: '分類管理' },
            ]),

            // ========== 培訓相關路由 ==========
            ...createRoutes('training', '培訓', [
                { path: 'trainProgramMan', component: 'trainProgramMan', title: '培訓計劃管理' },
                { path: 'courseMan', component: 'courseMan', title: '課程管理' },
                { path: 'gridMan', component: 'gridMan', title: '分類管理' },
            ])
        ]
    },
    //test

];

// 路由創建工廠函數
function createRoutes(topNav, category, routes) {
    return routes.map(route => ({
        path: `/${route.path}`, // 路由路徑
        component: () => import(`@/pages/${getComponentPath(topNav, route.component)}`), // 動態導入組件
        meta: { // 路由元信息
            topNav, // 所屬頂部導航 =============  首页或者菜单
            title: route.title, // 頁面標題 ================ 
            breadcrumb: createBreadcrumb([ // 生成麵包屑
                category, // 第一級為大類
                ...(route.breadcrumb || [route.title]) // 第二級為自定義或使用標題
            ])
        }
    }));
}

// 根據導航類型和組件名獲取組件路徑
function getComponentPath(topNav, componentName) {
    const folderMap = {
        home: 'index',
        organization: 'organizational',
        performance: 'roster',
        // 前面的參數是 ...createRoutes的第一個參數
        // 第二個參數是頁面的文件夾名字
        Attendance: 'Attendance',
        setting: 'setting',
        training: 'training'
    };

    const folder = folderMap[topNav] || topNav;
    return `${folder}/${componentName}.vue`; // 拼接完整路徑
}

// 生成麵包屑數據
function createBreadcrumb(titles) {
    return titles.map(title => ({ title }));
}

// 導出路由配置
export default routerConfig;